using System.Web;

namespace GeoWeb.Controls.WebGrid
{
    internal class WebGridHttpHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.Params["type"];
            string id = context.Request.Params["id"];
            string classId = context.Request.Params["classId"];
            WebGridDicItem item = WebGridCore.WebGrid_Dic.ContainsKey(classId + id) ? WebGridCore.WebGrid_Dic[classId + id] : null;

            context.Response.ClearContent();
            switch (type)
            {
                case "refresh":
                    context.Response.ContentType = "text/html";
                    context.Response.Write(WebGridCore.GetRefreshHTML_JSON(context, item));
                    break;
                case "editcell":
                    context.Response.ContentType = "text/html";
                    context.Response.Write(WebGridCore.Get_AfterEditCell(context, item) ? "true" : "");
                    break;
                case "script":
                    context.Response.ContentType = "text/javascript";
                    context.Response.Write(WebGridCore.Create_WebGrid_JS(item));
                    break;
                case "css":
                    context.Response.ContentType = "text/css";
                    context.Response.Write(WebGridCore.Create_WebGrid_DEFAULT_CSS());
                    break;
                case "image":
                    context.Response.ContentType = "image/bmp";
                    byte[] buffer = WebGridCore.Read_Image_To_BYTES(id);
                    context.Response.BinaryWrite(buffer);
                    break;
            }
        }
    }
}